
#import pepe.p_txt.bigtxt as bt
import base64
import string
import sys

def main(ifile):
    ''' encode or decode to/from base64
    '''
    #http://docs.python.org/release/2.3/lib/module-base64.html

    fname = string.split(ifile, '\x5C')[-1]

    # using ordinary read, not pepe.p_txt.bigtxt
    # because it has little practical use for big files

    #print ifile[-4:]

    if ifile[-3:] == 'b64':
        #print 'is b64, to be decoded'
        #print 'base64.decode(i, o)', fname, fname[:-4]
        i = open(fname, 'rb') 
        o = open(fname[:-4], 'wb')
        base64.decode(i, o)

    else:
        #print 'non b64, to be encoded'
        #print 'base64.encode(i, o)', fname, fname + '.b64'
        i = open(fname, 'rb') 
        o = open(fname + '.b64', 'wb')
        base64.encode(i, o)
   
    i.close()
    o.close()

if __name__ == "__main__":
    ''' '''
    if  len(sys.argv) == 2:
        main(ifile=sys.argv[1])
    else:
        print """
        Encode or decode to/from base64.
        
        b64 <input file>
        
        <input file>    if input file has extension b64 it will be decoded
                        if extension is not b64 it will be encoded
        """
